#!/bin/bash

set -eu

# script path
sd="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

hmsdir="${sd}"

scdir="${hmsdir}/scripts"
sf="${scdir}/shared_funcs.sh"

if [ -f "$sf" ]; then
    source "$sf"
else
    echo "Script file not found: $sf"
    echo "Exiting..."
    exit
fi

echo \
"This script configures the build and installation of hms.
First, you will be asked to choose some options, \
and then it proceeds with the configuration."

nChoices=4
echo "
Question 1 of ${nChoices}:
Which build configuration(s) do you wish to create?"
buildtypes=$(get_user_choice_def true \
"Release configuration only (for users)" \
"Release and Debug configuration (for developers)" \
"Debug configuration only")

echo "
Question 2 of ${nChoices}:
Do you wish to create aliases for the hms executable?
This will allow you to simply type hms in your command line, \
instead of specifying the whole path."
createalias=$(get_user_choice_def true \
"Yes, create aliases." \
"No, don't create aliases.")

echo "
Question 3 of ${nChoices}:
Do you wish to enable command completion for hms?
This will allow you to view and complete the available options \
by pressing the <tab> key."
enablecompl=$(get_user_choice_def true  \
"Yes, enable command completion." \
"No, don't enable command completion.")

echo "
Question 4 of ${nChoices}:
Do you wish to build hms after the configuration is complete?"
compile=$(get_user_choice_def true  \
"Yes, build hms." \
"No, do not build hms at this time.")

if (( $buildtypes >= 1 )); then
echo "
Bonus question for devs:
Do you wish to create shorthand commands for building hms?
This will allow you to enter
buildhms <type>
with <type> being 'Release', 'Debug' or 'all' (with auto-completion)."
buildshorthand=$(get_user_choice_def true \
"Yes, create shorthand commands for building hms." \
"No, do not create shorthand commands for building hms." )
fi

function require() {
    reqMissing=0
    for prog in "$@"
    do
        echo -n "  Checking $prog ... "
        if ! command -v "$prog" &> /dev/null
        then
            echo "NOT FOUND! Exiting after requirement check is finished."
            reqMissing=1
        else
            echo "OK"
        fi
    done
    if [ $reqMissing -eq 1 ]; then
        exit
    fi
}

echo -e "\nChecking prerequisites..."
require git g++ make cmake


echo -e "\nBuilding eigen library..."
buildEigen="${scdir}/build-eigen.sh"

if [ -f "$buildEigen" ]; then
    "${buildEigen}"
else
    echo "Eigen build script missing: ${buildEigen}"
    echo "Exiting..."
    exit
fi

if (( $buildtypes <= 1 )); then
    echo -e "\nConfiguring release build..."
    echo "Release dir: ${builddir_rel}"
    cmake -D CMAKE_BUILD_TYPE=Release -B "${builddir_rel}" -S "${hmsdir}"
fi
if (( $buildtypes >= 1 )); then
    echo -e "\nConfiguring debug build..."
    echo "Debug dir: ${builddir_deb}"
    cmake -D CMAKE_BUILD_TYPE=Debug -B "${builddir_deb}" -S "${hmsdir}"
fi

echo ""

# run register script in subshell to allow non-fatal exits
( "${scdir}/register_hms.sh" $buildtypes $createalias $enablecompl )

if (( $compile == 0 )); then
    nThreads=$(get_n_threads_build)

    if (( $buildtypes <= 1 )); then
        echo -e "\nBuilding release configuration with $nThreads thread(s). This may take several minutes."
        cd "${builddir_rel}"
        make -j $nThreads
    fi
    if (( $buildtypes >= 1 )); then
        echo -e "\nBuilding debug configuration with $nThreads thread(s). This may take several minutes."
        cd "${builddir_deb}"
        make -j $nThreads
    fi
fi
